/**
 * @author: dn-jinmin/dn-jinmin
 * @doc: 职责模式
 */

package main

import "fmt"

type Handler interface {
	SetNext(handler Handler) Handler
	Handle(request string) string
}

type BaseHandler struct {
	nextHandler Handler
}

func (bh *BaseHandler) SetNext(handler Handler) Handler {
	bh.nextHandler = handler
	return handler
}

type ConcreteHandler1 struct {
	BaseHandler
}

func (ch1 *ConcreteHandler1) Handle(request string) string {
	if request == "request1" {
		return "ConcreteHandler1 handled request1"
	} else if ch1.nextHandler != nil {
		return ch1.nextHandler.Handle(request)
	} else {
		return "No handler found for the request"
	}
}

type ConcreteHandler2 struct {
	BaseHandler
}

func (ch2 *ConcreteHandler2) Handle(request string) string {
	if request == "request2" {
		return "ConcreteHandler2 handled request2"
	} else if ch2.nextHandler != nil {
		return ch2.nextHandler.Handle(request)
	} else {
		return "No handler found for the request"
	}
}

func main1() {
	handler1 := new(ConcreteHandler1)
	handler2 := new(ConcreteHandler2)
	handler1.SetNext(handler2)

	fmt.Println(handler1.Handle("request1"))
	fmt.Println(handler1.Handle("request2"))
	fmt.Println(handler1.Handle("request3"))
}
